from 数据库操作.CreateMysqlCon import CreateMysqlCon

class LoginMysql:
    def __init__(self):
        self.con = CreateMysqlCon().get_con()
        if not self.con:
            raise ConnectionError("数据库连接失败，请检查配置或网络")
        self.cursor = self.con.cursor()

    def select_username_password(self, username, password):
        sql = "SELECT * FROM user_data WHERE username = %s AND password = %s;"
        self.cursor.execute(sql, (username, password))
        return self.cursor.fetchone()

    def judge(self, username, password):
        sql = "SELECT * FROM user_data WHERE username = %s AND password = %s;"
        result = self.cursor.execute(sql, (username, password))
        return result


    def select_username(self, username):
        sql = "SELECT * FROM user_data WHERE username = %s;"
        result = self.cursor.execute(sql, username)
        return result

    def insert_information(self, username, password, age, gender):
        if gender == '男':
            gender = 0
        elif gender == '女':
            gender = 1
        sql = "INSERT INTO user_data (username, password, age, gender) VALUES (%s, %s, %s, %s);"
        self.cursor.execute(sql, (username, password, int(age), gender))
